﻿#region Copyright information
// 
// Copyright © 2012-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the SubnetworkToolkit and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: PreProcessing.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@outlook.com
// - Date Created: 2013-01-30 2:55 PM
// - Last Modified: 2013-01-30 3:21 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BOL.Linq.Descriptive;

namespace SubnetworkToolkit.GeneticAlgorithmSearch
{
    public class PreProcessing
    {
        private List<SubnetGenome> _samples; 

        public int NumberOfSamples { get; private set; }

        public double Mean { get; private set; }

        public double StdDev { get; private set; }

        public double[] Means { get; private set; }

        public double[] StdDevs { get; private set; } 

        public PreProcessing(Random random, IDictionary<int, List<int>> dictionary, IDictionary<int, double> statistics, int minNodes, int maxNodes, int numberOfSamples)
        {
            if (dictionary == null)
                throw new ArgumentNullException("dictionary");
            if (statistics == null)
                throw new ArgumentNullException("statistics");

            var geneIds = dictionary.Keys.ToArray();
            NumberOfSamples = numberOfSamples;
            Mean = geneIds.Average(x => Math.Abs(statistics[x]));
            StdDev = geneIds.StandardDeviation(x => Math.Abs(statistics[x]));
            Console.WriteLine("{0}\t{1}", Mean, StdDev);

            _samples =
                new SubnetGenome[NumberOfSamples].Select(
                    x =>
                    new SubnetGenome(random, dictionary, statistics, new List<int> {geneIds[random.Next(maxNodes)]},
                                     minNodes, maxNodes, new List<int>(), 0, 0)).ToList();

            Means = new double[maxNodes];
            StdDevs = new double[maxNodes];

            for (var i = 0; i < maxNodes; i++)
            {
                var stat = _samples.Select(x => x.Ids.Average(y => Math.Abs(statistics[y]))).ToList();
                Means[i] = stat.Average();
                StdDevs[i] = stat.StandardDeviation();
                _samples.ForEach(x => x.Grow());
                Console.WriteLine("{0}\t{1}\t{2}", i + 1, Means[i], StdDevs[i]);
            }
        }
    }
}
